package com.sservice.web.controller.admin;

import com.sservice.common.Constants;
import com.sservice.common.SameUrlData;
import com.sservice.model.dao.*;
import com.sservice.urp.entity.User;
import com.sservice.urp.vo.UserVo;
import com.sservice.service.service.IOrderPaperService;
import com.sservice.service.service.ISysLogService;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.web.enums.Permissions;
import com.sservice.web.controller.websocket.WebsocketEndPoint;
import com.sservice.model.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by suweiming on 2017/12/20.
 */

@Controller
@RequestMapping("/admin/orderPaper")
public class OrderPaperController {

    @Autowired
    OrderPaperMapper maper;
    @Autowired
    IOrderPaperService service;
    @Autowired
    IOrderDao orderDao;
    @Autowired
    ILadingBillDao ladingBillDao;
    @Autowired
    PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    PurchaseOrderBillMapper purchaseOrderBillMapper;
    @Resource
    WebsocketEndPoint websocket;
    @Autowired
    ISysLogService sysLogService;

    /***
     *  save papper
     * @param op
     * @param session
     * @return
     */
    @ResponseBody
    @SameUrlData
    @RequestMapping(value = "/save-paper", method = RequestMethod.POST)
    public BaseResult savePaper(OrderPaper op, HttpSession session) {
        User user = (User) session.getAttribute(Constants.USER);
        OrderPaper op_res = maper.selectByOrderIdAndOrderType(op);
        int res = 0;
        if (op_res == null) {
            op.setUserId(String.valueOf(user.getId()));
            res = service.insertSelective(op);
        } else {
            op.setId(op_res.getId());
            // 变更
            res = service.updateByPrimaryKeySelective(op);
        }
        return new BaseResult(true, res);
    }

    /**
     * 申请签章
     *
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/apply/{orderId}/{orderType}", method = RequestMethod.POST)
    public BaseResult apply(OrderPaper arg, String visit, HttpSession session, HttpSession request) throws IOException {
        UserVo user = (UserVo) session.getAttribute(Constants.USER);
        OrderPaper op = maper.selectByOrderIdAndOrderType(arg);


        if (op == null) {
            return new BaseResult(false, "请先保存!", 403);
        }
        if ("0".equals(op.getSignatureStatus())) { // 已申请签章
            return new BaseResult(false, "签章申请中，不能再次申请!", 403);
        }
        if ("1".equals(op.getSignatureStatus())) { // 已申请签章
            return new BaseResult(false, "合同已授权签章，不能再次申请签章!", 403);
        }
        OrderPaper o1 = new OrderPaper();
        o1.setType(op.getOrderType());
        o1.setId(op.getId());
        o1.setSignatureStatus("0"); // 申请签章
        int res = service.updateByPrimaryKeySelective(o1);
        if (res > 0) {
            String no = "";
            if (op.getOrderType().equals("0")) {
                Order order = orderDao.selectByPrimaryKey(Integer.valueOf(op.getOrderId()));
                no = "【销售订单号：" + order.getNo() + "】";
            }
            if (op.getOrderType().equals("2")) {
                LadingBill order = ladingBillDao.selectByPrimaryKey(Integer.valueOf(op.getOrderId()));
                no = "【提货单号：" + order.getNo() + "】";
            }
            if (op.getOrderType().equals("1")) {
                PurchaseOrder order = purchaseOrderMapper.selectByPrimaryKey(Integer.valueOf(op.getOrderId()));
                no = "【采购订单号：" + order.getNo() + "】";
            }
            if (op.getOrderType().equals("3")) {
                PurchaseOrderBill order = purchaseOrderBillMapper.selectByPrimaryKey(Integer.valueOf(op.getOrderId()));
                no = "【收货单号：" + order.getNo() + "】";
            }
            websocket.sendMsgToJsp(no + "申请签章!", user, Permissions.PurchaseOrderAuthorizedSignature, visit);
            if ("1".equals(arg.getOrderType())) {
                PurchaseOrder po = purchaseOrderMapper.selectByPrimaryKey(Integer.valueOf(arg.getOrderId()));
                po.setApplySignature("1");// 申请签章
                purchaseOrderMapper.updateByPrimaryKeySelective(po);
            }
            return new BaseResult(true, "申请签章成功!", 200);
        }
        return new BaseResult(false, "申请签章失败!", 403);
    }

    /**
     * 授权签章
     *
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/signature/{orderId}/{orderType}", method = RequestMethod.POST)
    public BaseResult signature(OrderPaper arg, HttpSession session) {

        OrderPaper op = maper.selectByOrderIdAndOrderType(arg);
        if (!"0".equals(op.getSignatureStatus())) { // 已申请签章
            return new BaseResult(false, "合同非签章申请中状态，不能授权!", 403);
        }
        if ("1".equals(op.getSignatureStatus())) { // 已申请签章
            return new BaseResult(false, "合同已授权签章，不能授权签章!", 403);
        }
        OrderPaper o1 = new OrderPaper();
        o1.setType(op.getOrderType());
        o1.setId(op.getId());
        o1.setSignatureStatus("1"); // 申请签章
        int res = service.updateByPrimaryKeySelective(o1);
        if (res > 0) {
            if ("1".equals(arg.getOrderType())) {
                PurchaseOrder po = purchaseOrderMapper.selectByPrimaryKey(Integer.valueOf(arg.getOrderId()));
                po.setApplySignature("2");// 签章成功
                purchaseOrderMapper.updateByPrimaryKeySelective(po);
            }
            return new BaseResult(true, "授权签章成功!", 200);
        }
        return new BaseResult(false, "授权签章失败!", 403);
    }
}
